package com.xcyy.zjdxmjk.zjdxmjk.controller;

import com.xcyy.zjdxmjk.zjdxmjk.VO.Qjdc2VO;
import com.xcyy.zjdxmjk.zjdxmjk.VO.QjdcVO;
import com.xcyy.zjdxmjk.zjdxmjk.VO.ResultVO;
import com.xcyy.zjdxmjk.zjdxmjk.common.ResultVOUtil;
import com.xcyy.zjdxmjk.zjdxmjk.entity.City;
import com.xcyy.zjdxmjk.zjdxmjk.entity.Country;
import com.xcyy.zjdxmjk.zjdxmjk.entity.Town;
import com.xcyy.zjdxmjk.zjdxmjk.service.CityService;
import com.xcyy.zjdxmjk.zjdxmjk.service.CountryService;
import com.xcyy.zjdxmjk.zjdxmjk.service.HouseService;
import com.xcyy.zjdxmjk.zjdxmjk.service.TownService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
public class BdcclController {
    @Autowired
    private CityService cityService;
    @Autowired
    private CountryService countryService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private TownService townService;

    @PostMapping("/bdccl")
    private ResultVO bdccl(String code) {
        List<QjdcVO> qjdcVOS = new ArrayList<>();
        List<Qjdc2VO> qjdc2VOS = new ArrayList<>();
        String codestr;
        List<Object> objects;
        if ("0".equals(code)) {
            //获取所有市州
            List<City> citys = cityService.findAll();
            for (City city : citys) {
                QjdcVO qjdcVO = new QjdcVO();
                BeanUtils.copyProperties(city, qjdcVO);
                //获取市州code
                codestr = city.getCode().substring(0, 4);
                objects = houseService.countQjdcAndBdccl(codestr);
                for (Object o : objects) {
                    if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 0) {
                        qjdcVO.setZjd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                    } else if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 1) {
                        qjdcVO.setJsyd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                    }
                }
                qjdcVOS.add(qjdcVO);
            }
            return ResultVOUtil.success(qjdcVOS);
        } else if ("1".equals(code)) {
            //获取所有区县
            List<Country> countries = countryService.findAll();
            for (Country country : countries) {
                Qjdc2VO qjdc2VO = new Qjdc2VO();
                BeanUtils.copyProperties(country, qjdc2VO);
                //获得区县code
                codestr = country.getCode().substring(0, 6);
                objects = houseService.countQjdcAndBdccl(codestr);
                for (Object o : objects) {
                    if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 0) {
                        qjdc2VO.setZjd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                    } else if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 1) {
                        qjdc2VO.setJsyd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                    }
                }
                qjdc2VOS.add(qjdc2VO);
            }
            return ResultVOUtil.success(qjdc2VOS);

        }else if("2".equals(code)){
            //获取所有乡镇
            List<Town> towns=townService.findAll();
            for(Town town:towns){
                Qjdc2VO qjdc2VO = new Qjdc2VO();
                BeanUtils.copyProperties(town, qjdc2VO);
                //获得区县code
                codestr=town.getCode();
                objects=houseService.countQjdcAndBdccl(codestr);
                for (Object o : objects) {
                    if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 0) {
                        qjdc2VO.setZjd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                    } else if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 1) {
                        qjdc2VO.setJsyd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                    }
                }
                qjdc2VOS.add(qjdc2VO);
            }
            return ResultVOUtil.success(qjdc2VOS);

        }  else if (code.length() == 4) {
            //获取区县
            List<Country> countries = countryService.findByCodeStartsWith(code);
            for (Country country : countries) {
                QjdcVO qjdcVO = new QjdcVO();
                BeanUtils.copyProperties(country, qjdcVO);
                //获得区县code
                codestr = country.getCode().substring(0, 6);
                objects = houseService.countQjdcAndBdccl(codestr);
                for (Object o : objects) {
                    if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 0) {
                        qjdcVO.setZjd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                    } else if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 1) {
                        qjdcVO.setJsyd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                    }
                }
                qjdcVOS.add(qjdcVO);
            }
            return ResultVOUtil.success(qjdcVOS);
        } else if (code.length() == 6) {
            //获取乡镇
            List<Town> towns = townService.findByCodeStartsWith(code);
            for (Town town : towns) {
                QjdcVO qjdcVO = new QjdcVO();
                BeanUtils.copyProperties(town, qjdcVO);
                //获得乡镇code
                codestr = town.getCode().substring(0, 9);
                objects = houseService.countQjdcAndBdccl(codestr);
                for (Object o : objects) {
                    if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 0) {
                        qjdcVO.setZjd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                    } else if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 1) {
                        qjdcVO.setJsyd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                    }
                }
                qjdcVOS.add(qjdcVO);
            }
            return ResultVOUtil.success(qjdcVOS);
        }


        return ResultVOUtil.error(400, "行政区代码输入错误");
    }

    @PostMapping("/bdcclbyname")
    private ResultVO bdcclbyname(String name) {
        if (name.length() < 2) {
            return ResultVOUtil.error(403, "输入关键字太少，请至少输入两个字查询。");
        }
        List<Town> towns = townService.findByName(name);
        List<QjdcVO> qjdcVOS = new ArrayList<>();
        List<Object> objects;
        for (Town town : towns) {
            QjdcVO qjdcVO = new QjdcVO();
            BeanUtils.copyProperties(town, qjdcVO);
            objects = houseService.countQjdcAndBdccl(town.getCode());
            for (Object o : objects) {
                if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 0) {
                    qjdcVO.setZjd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                } else if ((Integer) (((Object[]) o)[0]) == 1 && (Integer) (((Object[]) o)[1]) == 1) {
                    qjdcVO.setJsyd(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
                }
            }
            qjdcVOS.add(qjdcVO);
        }

        return ResultVOUtil.success(qjdcVOS);
    }
}
